'use strict';
const dayjs = require("dayjs");
module.exports = app => {
    const { INTEGER, STRING, TEXT } = app.Sequelize;
    const LotteryRecord = app.model.define(
      'LotteryRecord',
          {
              id: {type: INTEGER, primaryKey: true, autoIncrement: true},
                user_id: INTEGER,
                record_id: INTEGER,
                product_id: INTEGER,
              track_no: STRING(255),
                status: {
                    type: INTEGER,
                },
                created_at: {
                  type: app.Sequelize.DATE,
                  allowNull: true,
                  get() {
                      return dayjs(this.getDataValue('created_at')).format('YYYY-MM-DD HH:mm:ss');
                      },
              },
              updated_at: {
                  type: app.Sequelize.DATE,
                  allowNull: true,
                  get() {
                      return dayjs(this.getDataValue('updated_at')).format('YYYY-MM-DD HH:mm:ss');
                  },
              }
          },
          {
              tableName: 'lottery_record',
              timestamps: true, // 启用时间戳(createdAt, updatedAt)
          }
    );
    LotteryRecord.associate = function() {
        app.model.LotteryRecord.belongsTo(app.model.User, {
            foreignKey: 'user_id',
            targetKey: 'id',
            as: 'user',
        });
        app.model.LotteryRecord.belongsTo(app.model.Product, {
            foreignKey: 'product_id',
            targetKey: 'id',
            as: 'product',
        });
    }
    return LotteryRecord;
};
